package com.yanwq.sayhi.controller.comment.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseDialogFragment;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.controller.comment.activity.CommentListActivity;
import com.yanwq.sayhi.controller.user.UserMgr;
import com.yanwq.sayhi.dao.bean.Article;
import com.yanwq.sayhi.dao.bean.Comment;
import com.yanwq.sayhi.dao.bean.Reply;
import com.yanwq.sayhi.dao.bean.User;
import com.yanwq.sayhi.widget.MToast;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommentAddFragment extends BaseDialogFragment {

    private EditText mContentV;
    private Article mArticle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_comment_add, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        initView();
    }

    private void init() {
        mArticle = (Article) getArguments().getSerializable("article");
    }

    private void initView() {
        mContentV = (EditText) findViewById(R.id.comment_content);

        if (getArguments().getInt("type") == 0) {
            mContentV.setHint("评论");
        } else {
            mContentV.setHint("回复");
        }

        findViewById(R.id.comment_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getArguments().getInt("type") == 0) {
                    sendComment();
                } else {
                    sendReply();
                }
            }
        });
    }

    private void sendComment() {
        if (TextUtils.isEmpty(mContentV.getText())) {
            MToast.showShort(getActivity(), "吐槽内容不能为空");
            return;
        }

        final Comment comment = new Comment();
        comment.setUser(UserMgr.get());
        comment.setArticle(mArticle);
        comment.setContent(mContentV.getText().toString());
        OkRestClient.getInstance().post(getActivity(),
                "/comment/add",
                comment,
                new OkJsonObjectResponseCallback() {
                    @Override
                    public void onStart() {
                        showProgressDialog("正在发表言论");
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        ((CommentListActivity) getActivity()).autoRefresh();
                        dismissAllowingStateLoss();
                    }

                    @Override
                    public void onStop() {
                        cancelProgressDialog();
                    }
                });
    }

    private void sendReply() {
        if (TextUtils.isEmpty(mContentV.getText())) {
            MToast.showShort(getActivity(), "吐槽内容不能为空");
            return;
        }

        final Reply reply = new Reply();
        Comment comment = (Comment) getArguments().getSerializable("comment");
        if (comment != null) {
            reply.setComment(comment);
        }
        reply.setFromUser(UserMgr.get());

        User toUser = (User) getArguments().getSerializable("toUser");
        if (toUser != null) {
            reply.setToUser(toUser);
        }

        reply.setContent(mContentV.getText().toString());
        reply.getComment().setReplies(null);
        OkRestClient.getInstance().post(getActivity(),
                "/reply/add",
                reply,
                new OkJsonObjectResponseCallback() {
                    @Override
                    public void onStart() {
                        showProgressDialog("正在发表言论");
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        ((CommentListActivity) getActivity()).autoRefresh();
                        dismissAllowingStateLoss();
                    }

                    @Override
                    public void onStop() {
                        cancelProgressDialog();
                    }
                });
    }
}
